﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LinqToPiWCFTest.PiDataService;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace LinqToPiWCFTest
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<PiValue> values;

        public DateTime SelectedStartTime
        {
            get
            {
                return startDatePicker.SelectedDate.Value.Date + startTimePicker.Value.Value.TimeOfDay;
            }
        }

        public DateTime SelectedEndTime
        {
            get
            {
                return endDatePicker.SelectedDate.Value.Date + endTimePicker.Value.Value.TimeOfDay;
            }
        }

        public MainPage()
        {
            InitializeComponent();

            startDatePicker.SelectedDate = DateTime.Now;
            endDatePicker.SelectedDate = DateTime.Now.AddDays(-1);
            startTimePicker.Value = DateTime.Now;
            endTimePicker.Value = DateTime.Now;
            chartType.ItemsSource = new Type[] { typeof(LineSeries), typeof(AreaSeries), typeof(ScatterSeries) };
            chartType.SelectedIndex = 0;
        }

        private void searchBtn_Click(object sender, RoutedEventArgs e)
        {
            PiDataServiceClient client = new PiDataServiceClient();
            client.GetPiValuesCompleted +=new EventHandler<GetPiValuesCompletedEventArgs>(client_GetPiValuesCompleted);
            client.GetPiValuesAsync(
                containsTxt.Text, 
                SelectedStartTime,
                SelectedEndTime);
        }

        void client_GetPiValuesCompleted(object sender, GetPiValuesCompletedEventArgs e)
        {
            values = e.Result;
           
            RefreshData();
        }

        private void RefreshData()
        {
            resultGrid.ItemsSource = values;

            // areaSeries.ItemsSource = e.Result;
            resultChart.Series.Clear();
            if (values != null && values.Count > 0)
            {
                var series = GetSerie();

                series.IndependentValueBinding = new Binding("Time");
                series.DependentValueBinding = new Binding("Value");
                series.ItemsSource = values;
                series.Title = values[0].Tag;
                resultChart.Series.Add(series);
            }
        }

        private DataPointSeries GetSerie()
        {
            return Activator.CreateInstance(chartType.SelectedItem as Type) as DataPointSeries;
        }

        private void chartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshData();
        }

        private void resultGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
        {
            PiValue editedValue = resultGrid.SelectedItem as PiValue;
            PiDataServiceClient client = new PiDataServiceClient();
            client.UpdatePiValueAsync(editedValue);
        }

    
      
    }
}
